﻿using System;
using UnityEngine;

public class Sun : MonoBehaviour, IEditable
{
    public Color ambientColor = ((Color) (Color.white * 0.5f));
    private Light m_light;
    private Model model;
    public Color sunColor = Color.white;
    public Vector3 sunDirection = Vector3.down;

    private void Awake()
    {
        this.model = base.GetComponent<Model>();
        this.m_light = base.GetComponentInChildren<Light>();
    }

    private void OnDisable()
    {
        RenderSettings.ambientLight = (Color) (Color.white * 0.1f);
    }

    public void OnEditorGUI()
    {
        this.sunDirection = GameGUI.Vector3Field("太阳方向", this.sunDirection);
        this.sunColor = GameGUI.ColorField("太阳颜色", this.sunColor, false);
        this.ambientColor = GameGUI.ColorField("环境颜色", this.ambientColor, false);
        if (GUI.changed)
        {
            this.Refresh();
        }
    }

    private void Refresh()
    {
        RenderSettings.ambientLight = this.ambientColor;
        this.m_light.color = this.sunColor;
        if (this.sunDirection != Vector3.zero)
        {
            this.m_light.transform.localRotation = Quaternion.LookRotation(this.sunDirection);
            if ((this.model != null) && (this.model.instance != null))
            {
                this.model.instance.transform.localRotation = this.m_light.transform.localRotation;
            }
        }
        this.m_light.enabled = this.m_light.color != new Color(0f, 0f, 0f, this.m_light.color.a);
    }

    private void Start()
    {
        this.Refresh();
    }
}

